# -*- coding: utf-8 -*-
'''
Copyright(C) 2013 "Kevin Xu <ssonxu@gmail.com>"

This file is part of acqu-center.

acqu-center is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

acqu-center is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with acqu-center.  If not, see <http://www.gnu.org/licenses/>.
'''
import webapp2
import logging
from google.appengine.api import xmpp
from google.appengine.ext.webapp import xmpp_handlers
from modules.box import Box
from modules.contact import Contact as Con
from modules.Predef import Predef as PRE

HELLO_MESSAGE=u"Hello, I am acqu center robot.\n We will share some interesting news if you like."
UNHANDLED_MESSAGE=u"Hey buddy, I don't know what are you talking about. Please speak something I knew.\n If you want help just type \\ask, have a good day ^_^"
MENU_MESSAGE=u"That is is a menu, please order:\n{}"
NOFOUND_MESSAGE=u"Sorry, I know nothing about {}:\n, Maybe you can try \\menu first\n"
NOREPORT_MESSAGE=u"Sorry, no special, no news about that.\n"

class xmppHandler(xmpp_handlers.CommandHandler):

    def processing(self, message, label):
        if label and len(label)>0:
            i = PRE.atoi(label)
            if i>=0:
                b = Box.getBoxByNo(i)
            else:
                b = Box.getBox(label)

            if b is not None:
                report=b.report(isSkipMatch=True)
                if  len(report)>0:
                    message.reply(report)
                else:
                    message.reply(NOREPORT_MESSAGE)
            else:
                message.reply(NOFOUND_MESSAGE.format(label))
        else:
            message.reply(MENU_MESSAGE.format(Box.getMenu()))

    def unhandled_command(self, message=None):
        logging.info("unhandled_command")
        message.reply(UNHANDLED_MESSAGE,raw_xml=False)

    def text_message(self, message=None):
        logging.info("text_message")
        #message.reply(UNHANDLED_MESSAGE,raw_xml=False)
        self.processing( message, message.body.strip() )

    def menu_command(self, message=None):
        logging.info("menu_command")
        message.reply(MENU_MESSAGE.format(Box.getMenu()))

    def ask_command(self, message=None):
        label=message.body[5:]
        self.processing( message, label )

class xmppPresenceHandler(webapp2.RequestHandler):
    def post(self,status):
        sender = self.request.get('from')
        if status == 'available':
            logging.info(sender+" added")
            Con.addUser(sender)

app = webapp2.WSGIApplication([
    ('/_ah/xmpp/message/chat/', xmppHandler),
    ('/_ah/xmpp/presence/(available|unavailable)/', xmppPresenceHandler),
    ], debug=True)
